from sqlalchemy.sql.functions import current_user

from info import redis_store
from info.models import User, News, Category
from info.utils.commons import user_login_data
from info.utils.response_code import RET
from . import index_blu
from flask import render_template, current_app, session, jsonify, request, g, redirect, url_for
import collections


flag_num = 0

#功能描述: 首页新闻列表获取
# 请求路径: /newslist
# 请求方式: GET
# 请求参数: cid,page,per_page
# 返回值: data数据
@index_blu.route('/newslist')
@user_login_data
def news_list():
    """
    1.获取参数
    2.参数类型转换,为了分页 做准备,paginate(page,per_page,False)
    3.分页查询
    4.取出分页对象中的属性,总页数,当前页,当前页对象
    5.将当前页对象列表,转成字典列表
    6.返回响应
    :return:
    """
    global flag_num
    # 1.获取参数
    cid = request.args.get("cid","1")
    page = request.args.get("page","1")
    per_page = request.args.get("per_page","10")
    
    # 2.参数类型转换,为了分页 做准备,paginate(page,per_page,False)
    try:
        page = int(page)
        per_page = int(per_page)
    except Exception as e:
        page = 1
        per_page = 10

    # 2.1判断是否为猜你喜欢
    if cid == "8":
        #如果未登录
        if not g.user:
            filters = [News.status == 0]
            paginate = News.query.filter(*filters).order_by(News.create_time.desc()).paginate(page, per_page, False)

            # 2.2.取出分页对象中的属性,总页数,当前页,当前页对象
            totalPage = paginate.pages
            currentPage = paginate.page
            items = paginate.items

            # 2.3.将当前页对象列表,转成字典列表
            newsList = []
            for item in items:
                newsList.append(item.to_dict())
            print(news_list)
            # 2.4.返回响应
            return jsonify(errno=RET.OK, errmsg="获取成功", totalPage=totalPage, currentPage=currentPage, newsList=newsList)
        #如果登录
        alist = []
        #获取用户收藏新闻的id
        try:
            uuu = g.user.collection_news

            for i in uuu:
                alist.append(i.return_id())
        except Exception as e:
            current_app.logger.error(e)
        #获取用户收藏新闻分类的数量
        b = collections.Counter(alist)
        clist = {}
        for c in b:
            clist[c]=b[c]
        #获取收藏最高的新闻分类id
        # ccid = int(max(clist, key=clist.get))
        #对新闻分类做排序
        d2 = sorted(b.items(), key=lambda d: d[1], reverse=True)
        #防止下标越界
        if flag_num > len(d2)-1:
            flag_num=0
        try:
            filters = [News.status == 0]
            if len(alist) == 0:
                pass
            else:
                filters.append(News.category_id == d2[flag_num][0])
            paginate = News.query.filter(*filters).order_by(News.create_time.desc()).paginate(page, per_page, False)
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR,errmsg='查询错误')
        #增加迭代，让下次查询不同
        flag_num += 1
        # 4.取出分页对象中的属性, 总页数, 当前页, 当前页对象
        totalPage = paginate.pages
        currentPage = paginate.page
        items = paginate.items

        # 5.将当前页对象列表,转成字典列表
        newsList = []
        for item in items:
            newsList.append(item.to_dict())
        # 6.返回响应
        return jsonify(errno=RET.OK, errmsg="获取成功", totalPage=totalPage, currentPage=currentPage, newsList=newsList)

    # 3.分页查询
    try:
        #判断是否cid != 1, 不是最新
        filters = [News.status == 0]
        if cid != "1":
            filters.append(News.category_id == cid)
        paginate = News.query.filter(*filters).order_by(News.create_time.desc()).paginate(page,per_page,False)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取新闻失败")
    
    # 4.取出分页对象中的属性,总页数,当前页,当前页对象
    totalPage = paginate.pages
    currentPage = paginate.page
    items = paginate.items

    # 5.将当前页对象列表,转成字典列表
    newsList = []
    for item in items:
        newsList.append(item.to_dict())
    # 6.返回响应
    return jsonify(errno=RET.OK,errmsg="获取成功",totalPage=totalPage,currentPage=currentPage,newsList=newsList)


@index_blu.route('/')
@user_login_data
def show_index():

    #2.1 热门新闻,按照新闻的点击量量,查询前十条新闻
    try:
        news_list = News.query.order_by(News.clicks.desc()).limit(10).all()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR,errmsg="获取新闻失败")

    #2.2 将新闻列表对象,字典列表对象
    click_news_list = []
    for news in news_list:
        click_news_list.append(news.to_dict())

    #2.3 查询所有分类信息
    try:
        categories = Category.query.all()
    except Exception as e:
        current_app.logger.error(e)

    #2.4 将分类对象列表,转成字典列表
    category_list = []
    for category in categories:
        category_list.append(category.to_dict())

    #3.拼接用户数据渲染页面
    data = {
        #如果user不为空,返回左边的内容, 为空返回右边内容
        "user_info":g.user.to_dict() if g.user else "",
        "click_news_list":click_news_list,
        "categories":category_list
    }

    return render_template("news/index.html",data=data)

#处理网站logo
#每个浏览器在访问服务器的时候,会自动向该服务器发送GET请求,地址是:/favicon.ico
#获取静态文件夹资源: current_app.send_static_file("文件路径"),会自动寻找static静态文件夹的资源
@index_blu.route('/favicon.ico')
def web_logo():

    return current_app.send_static_file("news/favicon.ico")

@index_blu.route('/media_login')
def media_login():
    return render_template('news/media_login.html')